package net.mikoo.seals.api.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.avalon.framework.parameters.ParameterException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.api.model.BaseResponse;
import net.mikoo.seals.api.model.ResultType;
import net.mikoo.seals.api.model.seal.AddSealRequest;
import net.mikoo.seals.api.model.seal.EnabledSealRequest;
import net.mikoo.seals.api.model.seal.SealDetailRequest;
import net.mikoo.seals.api.model.seal.SealDetailResponse;
import net.mikoo.seals.api.model.seal.SealListRequest;
import net.mikoo.seals.api.model.seal.SealListRequest.PurposeEnum;
import net.mikoo.seals.api.model.seal.SealListResponse;
import net.mikoo.seals.api.model.seal.UpdateSealRequest;
import net.mikoo.seals.api.utility.PartyUtility;
import net.mikoo.seals.biz.service.SealServices;
import net.mikoo.seals.common.enums.DevicesStatusEnum;
import net.mikoo.seals.common.exception.NeedBackMsgException;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.model.Seal;
import net.mikoo.seals.model.SealQuery;

@RequestMapping
@Controller
public class SealController {

	private static Logger logger = LoggerFactory.getLogger(SealController.class);
	
	@Autowired
	private SealServices sealServices;
	
	@RequestMapping("/seal/list")
	@ResponseBody
	public SealListResponse list(SealListRequest request, HttpServletRequest httpRequest) {
		SealListResponse result = new SealListResponse();
		try {
			if(request == null) {
				throw new ParameterException("request");
			}
			if(request.getPageNo() == null || request.getPageNo() < 1) {
				request.setPageNo(1);
			}
			if(request.getPageSize() == null || request.getPageSize() < 1) {
				request.setPageSize(10);
			}
			
			PurposeEnum purpose = PurposeEnum.getByCode(request.getPurpose());
			SealQuery query = new SealQuery();
			if(purpose == PurposeEnum.ADMIN_SEAL_LIST) {
				query.setPartyId(PartyUtility.getCurrentPartyId(httpRequest));
			}
			
			query.setSealName(request.getSealName());
			query.setSealStatus(request.getSealStatus());
			query.setNoDevicesStatus(DevicesStatusEnum.DESERTED.getCode());
			query.setOffset((request.getPageNo() - 1)*request.getPageSize());
			query.setPageSize(request.getPageSize());
			query.setCompanyId(PartyUtility.getCurrentPartyCompanyId(httpRequest));
			
			PageModel<Seal> pm = sealServices.getPageByQuery(query);
			result = SealListResponse.valueOf(pm);
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(Exception e) {
			logger.error("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		return result;
	}
	
	@RequestMapping("/seal/add")
	@ResponseBody
	public BaseResponse add(AddSealRequest request, HttpServletRequest httpRequest) {
		BaseResponse result = new BaseResponse();
		try {
			if(request == null) {
				throw new ParameterException("requset");
			}
			
			if(StringUtils.isBlank(request.getSealName())) {
				throw new ParameterException("SealName");
			}
			
			if(request.getCustosPartyId() == null) {
				throw new ParameterException("CustosPartyId");
			}
			
			if(StringUtils.isBlank(request.getIsForcedPhoto())) {
				throw new ParameterException("IsForcedPhoto");
			}
			
			Seal seal = new Seal();
			seal.setCompanyId(PartyUtility.getCurrentPartyCompanyId(httpRequest));
			seal.setPartyId(request.getCustosPartyId());
			seal.setSealName(request.getSealName());
			seal.setIsForcedPhoto(request.getIsForcedPhoto());
			seal.setLimitUrgentApply(request.getLimitUrgentApply());
			sealServices.save(seal);
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(Exception e) {
			logger.error("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		
		return result;
	}
	
	
	@RequestMapping("/seal/update")
	@ResponseBody
	public BaseResponse update(UpdateSealRequest request, HttpServletRequest httpRequest) {
		BaseResponse result = new BaseResponse();
		try {
			if(request == null) {
				throw new ParameterException("requset");
			}
			
			if(request.getSealId() == null) {
				throw new ParameterException("sealId");
			}
			
			if(StringUtils.isBlank(request.getSealName())) {
				throw new ParameterException("SealName");
			}
			
			if(request.getCustosPartyId() == null) {
				throw new ParameterException("CustosPartyId");
			}
			
			if(StringUtils.isBlank(request.getIsForcedPhoto())) {
				throw new ParameterException("IsForcedPhoto");
			}
			
			Seal seal = new Seal();
			seal.setSealId(request.getSealId());
			seal.setCompanyId(PartyUtility.getCurrentPartyCompanyId(httpRequest));
			seal.setPartyId(request.getCustosPartyId());
			seal.setSealName(request.getSealName());
			seal.setIsForcedPhoto(request.getIsForcedPhoto());
			seal.setLimitUrgentApply(request.getLimitUrgentApply());
			sealServices.modify(seal);
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(Exception e) {
			logger.error("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		return result;
	}
	
	
	@RequestMapping("/seal/detail")
	@ResponseBody
	public SealDetailResponse detail(SealDetailRequest request, HttpServletRequest httpRequest) {
		SealDetailResponse result = new SealDetailResponse();
		try {
			
			if(request == null) {
				throw new ParameterException("request");
			}
			
			if(request.getSealId() == null) {
				throw new ParameterException("request.sealId");
			}
			
			Seal seal = sealServices.getInfoById(request.getSealId());
			
			result = SealDetailResponse.valueOf(seal);
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(Exception e) {
			logger.debug("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		return result;
	}
	
	@RequestMapping("/seal/enabled")
	@ResponseBody
	public BaseResponse toEnabled(EnabledSealRequest request, HttpServletRequest httpRequest) {
		BaseResponse result = new BaseResponse();
		try {
			
			if(request == null) {
				throw new ParameterException("request");
			}
			
			if(request.getSealId() == null) {
				throw new ParameterException("request.sealId");
			}
			
			Seal seal = new Seal();
			seal.setSealId(request.getSealId());
			seal.setCompanyId(PartyUtility.getCurrentPartyCompanyId(httpRequest));
			sealServices.changeStatus(seal);
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(NeedBackMsgException ne) {
			logger.debug("", ne);
			result.setResult(ResultType.ERROR.getValue());
			result.setErrorMsg(ne.getMessage());
		}catch(Exception e) {
			logger.debug("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		return result;
	}
	
}
